package com.ifast.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.base.ApiBaseController;
import com.ifast.news.domain.News;
import com.ifast.news.domain.NewsCollect;
import com.ifast.news.service.NewsService;
import com.ifast.sys.domain.UserDO;
import com.luculent.util.extension.api.PageResult;
import com.luculent.util.extension.api.Result;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;

/**
 * <pre>
 *   新闻接口
 * </pre>
 * 
 * <small>2019年4月10日15:28:01 | ShiQinag</small>
 */
@RestController
@RequestMapping("/api/news/")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ApiNewsController extends ApiBaseController{
	
	@Autowired
    private NewsService newsService; 
   
	@RequestMapping(value = "classify.do")
    @ApiOperation( value = "新闻分类", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> classify(@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {
    		return newsService.classify(getUser(token).getId().toString());
        } catch (Exception e) { 
        	e.printStackTrace();
			return Result.fail_msg("查询异常");
		}
    } 
	
	@RequestMapping(value = "classifyHelp.do")
    @ApiOperation( value = "手册", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> classifyHelp(@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {
    		return newsService.newsHelp(getUser(token).getId().toString());
        } catch (Exception e) { 
        	e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    } 
	
	@RequestMapping(value = "classifyAboutOurs.do")
    @ApiOperation( value = "关于我们", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> classifyAboutOurs(@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {
    		return newsService.newsAboutOurs(getUser(token).getId().toString());
        } catch (Exception e) { 
        	e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    }
	
	@PostMapping(value = "list.do")
    @ApiOperation( value = "新闻列表", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    
    public PageResult<?> list(
    		@ApiParam(name = "title", required = false, value = "标题") @RequestParam(value = "title",required=false) String title,
    		@ApiParam(name = "classify", required = false, value = "分类") @RequestParam(value = "classify",required=false) String classify,
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageSize", required = false, value = "每页多少条") @RequestParam(value = "pageSize",required=false) String pageSize,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
        try { 
    		UserDO user = getUser(token);
         	Page<News> page = new Page<>();
         	//每页多少条
         	if(StringUtils.isNoneBlank(pageSize)){
         		page.setSize(Integer.valueOf(pageSize));
         	} 
            page.setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 );
            return newsService.pageList(user.getId().toString(),user.getDwCode(),title,classify,nian,yue,ri,page);
        } catch (Exception e) {
        	e.printStackTrace();
			return PageResult.fail_msg("查询异常");
		}
    } 
	 
	
	@PostMapping(value = "helpList.do")
    @ApiOperation( value = "系统说明列表", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    
    public PageResult<?> helpList(
    		@ApiParam(name = "title", required = false, value = "标题") @RequestParam(value = "title",required=false) String title,
    		@ApiParam(name = "classify", required = false, value = "分类") @RequestParam(value = "classify",required=false) String classify,
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageSize", required = false, value = "每页多少条") @RequestParam(value = "pageSize",required=false) String pageSize,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
        try { 
    		UserDO user = getUser(token);
         	Page<News> page = new Page<>();
         	//每页多少条
         	if(StringUtils.isNoneBlank(pageSize)){
         		page.setSize(Integer.valueOf(pageSize));
         	} 
            page.setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 );
            return newsService.pageHelpList(user.getId().toString(),user.getDwCode(),title,classify,nian,yue,ri,page);
        } catch (Exception e) {
        	e.printStackTrace();
			return PageResult.fail_msg("查询异常");
		}
    }
	
	
	@RequestMapping(value = "info.do")
    @ApiOperation( value = "新闻详情", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> info(
    		@ApiParam(name = "newsId", required = true, value = "id") @RequestParam(value = "newsId",required=true) String newsId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {  
    		return newsService.info(getUser(token).getId().toString(),newsId);
        } catch (Exception e) { 
        	e.printStackTrace();
			return Result.fail_msg("查询异常");
		}
    } 
	
	@RequestMapping(value = "collectList.do")
    @ApiOperation( value = "收藏的新闻列表", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    
    public PageResult<?> collectList(
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageSize", required = false, value = "每页多少条") @RequestParam(value = "pageSize",required=false) int pageSize,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
        try { 
    		UserDO user = getUser(token);
         	Page<NewsCollect> page = new Page<>();
         	//每页多少条
         	if(pageSize != 0){
         		page.setSize(pageSize);
         	} 
            page.setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 );
            return newsService.pageCollectList(user.getId().toString(),user.getDwCode(),nian,yue,ri,page);
        } catch (Exception e) {
        	e.printStackTrace();
			return PageResult.fail_msg("查询异常");
		}
    } 
	
	@RequestMapping(value = "collect.do")
    @ApiOperation( value = "收藏", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> collect(
    		@ApiParam(name = "newsId", required = true, value = "id") @RequestParam(value = "newsId",required=true) String newsId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {  
    		return newsService.collect(getUser(token).getId().toString(),newsId);
        } catch (Exception e) { 
        	e.printStackTrace();
			return Result.fail_msg("查询异常");
		}
    } 
	
	@RequestMapping(value = "unCollect.do")
    @ApiOperation( value = "取消收藏", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> unCollect(
    		@ApiParam(name = "newsId", required = true, value = "id") @RequestParam(value = "newsId",required=true) String newsId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {  
    		return newsService.unCollect(getUser(token).getId().toString(),newsId);
        } catch (Exception e) { 
        	e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    } 
	
	@RequestMapping(value = "click.do")
    @ApiOperation( value = "点赞", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> click(
    		@ApiParam(name = "newsId", required = true, value = "id") @RequestParam(value = "newsId",required=true) String newsId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {  
    		return newsService.click(getUser(token).getId().toString(),newsId);
        } catch (Exception e) { 
        	e.printStackTrace();
			return Result.fail_msg("查询异常");
		}
    } 
	
	@RequestMapping(value = "unClick.do")
    @ApiOperation( value = "取消点赞", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> unClick(
    		@ApiParam(name = "newsId", required = true, value = "id") @RequestParam(value = "newsId",required=true) String newsId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {  
    		return newsService.unClick(getUser(token).getId().toString(),newsId);
        } catch (Exception e) { 
			e.printStackTrace();
			return Result.fail_msg("查询异常");
		}
    } 
}
